package com.situ.ssh.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.situ.ssh.common.ServerResponse;
import com.situ.ssh.dao.IUserDao;
import com.situ.ssh.entity.User;
import com.situ.ssh.vo.SearchCondition;

import net.sf.json.JSONObject;

@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements IUserDao{

	@Override
	public User login(User user) {
		String hql = "from User where name=? and password=?";
		List<User> list = (List<User>) getHibernateTemplate().find(hql, user.getName(), user.getPassword());
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ServerResponse pageList(SearchCondition searchCondition) {
		Integer page = searchCondition.getPage();
		Integer limit = searchCondition.getLimit();
		DetachedCriteria detachedCriteria = searchCondition.getDetachedCriteria();
		//查询total:总记录数(带着条件查询出来的总记录数)
		detachedCriteria.setProjection(Projections.rowCount());//要查询总记录数
		List<?> list = getHibernateTemplate().findByCriteria(detachedCriteria);
		long count = (long) list.get(0);
		//得到当前页的数据
		detachedCriteria.setProjection(null);
		int firstResult = (page - 1) * limit;
		List<User> rows = (List<User>) getHibernateTemplate().findByCriteria(detachedCriteria, firstResult,limit);
		
		return ServerResponse.createSuccess("查找成功", (int)count, rows);
	}

}
